<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>


<body>
<div id="one"></div>
<div id="two"></div>
    <script type="text/javascript">
    //demo1
	/*  new Promise(
    (resolve, reject)=>{
        var n = 1;
        console.log(n);
        let One = document.getElementById('one');
        One.innerHTML = n;
        resolve(n);
        // console.log(resolve(n))
    })
    .then((data)=>{
        console.log(data)
        let Three = document.getElementById('three');
        Three.innerHTML = data;
        return data += data;
    })
    .then((data)=>{
        console.log(data)
        let Two = document.getElementById('two');
        Two.innerHTML = data;
        return data += data;
    })
    .then((data)=>{
        // console.log(data)
        let One = document.getElementById('one');
        var hh = One.innerHTML;
        console.log(hh + 'test');
        return data += data;
    })
    .catch((error)=>{
        console.log(error)
    })*/


    //demo2
  /*  var p1 = new Promise(function (resolve) {
	    setTimeout(function () {
	        resolve("1");
	    }, 3000);
	});
	var p2 = new Promise(function (resolve) {
	    setTimeout(function () {
	        resolve("2");
	    }, 2000);
	});

	var p3 = new Promise(function (resolve) {
	    setTimeout(function () {
	        resolve("3");
	    }, 1000);
	});*/

	//多条ajax请求可以同时放到数组中去，执行时按数组的顺序执行
	/*Promise.all([p1, p2, p3]).then(function (result) {
	    console.log(result); // ["1", "2", "3"]
	});*/

	//谁先满足条件谁先执行，后边忽略掉
	/*Promise.race([p1, p2, p3]).then(function (result) {
	    console.log(result); // '3'
	});*/

	//resolve(n)的理解，resolve 方法可以使 Promise 对象的状态改变成成功，同时传递一个参数用于后续成功后的操作，then中的result接收的值就是resolve中传递的值
	//reject(n)的理解， reject  方法则是将 Promise 对象的状态改变为失败，参数的作用与resolve相同。

	//demo3
	function helloWorld(ready) {
	    return new Promise(function (resolve, reject) {
	        if (ready) {
		            resolve("Hello World!");
		        } else {
		            reject("Good bye!");
		        }
		    });
		}
		
	//console.log(helloWorld(false))

	helloWorld(false).then((message)=>{
	    alert(message);
	}).catch((error)=>{
		alert(error);
	});
	// Promise.all(helloWorld(true)).then((message)=>{
	//     alert(message);
	// }).catch((error)=>{
	// 	alert(error);
	// });
    </script>
</body>

</html>